﻿using System.Drawing;
using System.IO;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.ADF.BaseClasses;
using System.Runtime.InteropServices;
using PlanningGIS.Statistics;
using PlanningGIS.Framework;

namespace PlanningGIS.Statistics
{
    /// <summary>
    /// 要素统计按钮类
    /// 作者：zly
    /// 编写日期：2010-04-17
    /// </summary>
    public class YSSLTJButton : BaseCommand
    {
            
        IApplication m_Application = null;

        public YSSLTJButton()
        {
            base.m_caption = "要素统计";
            base.m_toolTip = base.m_caption;
        }

        public override void OnCreate(object hook)
        {
            if (m_Application == null)
            {
                m_Application = hook as IApplication;
                //base.OnCreate(hook);
            }
        }

        public override bool Enabled
        {
            get
            {
                
                if ((m_Application as IFramework).IsMapCtrlactive)
                {
                    IEnumLayer layers = (m_Application.Document as IMxDocument).ActiveView.FocusMap.get_Layers(null, true);
                    ILayer layer = layers.Next();
                    while (layer != null)
                    {
                        if (layer is IFeatureLayer)
                        {
                            Marshal.ReleaseComObject(layers);
                            return true;
                        }
                        layer = layers.Next();
                    }
                    Marshal.ReleaseComObject(layers);
                }
                return false;
            }
        }

        public override void OnClick()
        {
            if ((m_Application as IFramework).Tool == this) return;
            base.OnClick();

            IMapControl3 pMapControl = null;
            if ((m_Application as IFramework).IsMapCtrlactive)
                pMapControl = (m_Application as IFramework).MapControl;
            else
                return;

            if (pMapControl == null || pMapControl.Map.LayerCount == 0) return;

            frmYSSLTJ frm = new frmYSSLTJ();
            frm.m_Application = m_Application;
            frm.m_pMap = pMapControl.Map ;
            frm.Show();
        }

        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = "PlanningGIS.Statistics.Res." + this.GetType().Name + ".png";
                System.IO.Stream s = GetType().Assembly.GetManifestResourceStream(bitmapResourceName);
                if (s == null) return 0;
                return new Bitmap(s).GetHbitmap().ToInt32(); 
            }
        }

    }
}